package com.jspxcms.plug.web.fore;

import com.jspxcms.common.util.JsonResponse;
import com.jspxcms.core.domain.Site;
import com.jspxcms.core.support.Context;
import com.jspxcms.core.support.ForeContext;
import com.jspxcms.plug.domain.PlugDisease;
import com.jspxcms.plug.domain.PlugDoctor;
import com.jspxcms.plug.domain.PlugHospital;
import com.jspxcms.plug.domain.PlugHospitalDepartment;
import com.jspxcms.plug.service.PlugDiseaseService;
import com.jspxcms.plug.service.PlugDoctorService;
import com.jspxcms.plug.service.PlugHospitalDepartmentService;
import com.jspxcms.plug.service.PlugHospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * ResumeController
 *
 * @author liufang
 */
@Controller
public class HospitalController {
    public static final String TEMPLATE = "2—2名医名院1.html";

    @RequestMapping(value = "/hospital/index.jspx")
    public Object hospital_index(HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Site site = Context.getCurrentSite(request);
        Map<String, Object> data = modelMap.asMap();
        ForeContext.setData(data, request);
        return site.getTemplate(TEMPLATE);
    }

    @RequestMapping(value = "/hospital/search_ajax.jspx")
    public Object search(Integer provinceid, Integer cityid, Integer areaid,
                         String name,
                         Integer limit,
                         HttpServletRequest request,
                         HttpServletResponse response) {
        response.setHeader("Content-type", "text/html");
        Map<String, String[]> maps = new HashMap<String, String[]>();
        if (name != null && name.length() > 0) {
            maps.put("CONTAIN_fHospitalName", new String[]{name});
        }
        if (provinceid != null && provinceid > 0) {
            maps.put("EQ_fProvinceId", new String[]{provinceid.toString()});
        }
        if (cityid != null && cityid > 0) {
            maps.put("EQ_fCityId", new String[]{cityid.toString()});
        }
        if (areaid != null && areaid > 0) {
            maps.put("EQ_fAreaId", new String[]{areaid.toString()});
        }
        if (limit == null || limit == 0) {
            limit = Integer.MAX_VALUE;
        }
        Pageable page = new PageRequest(0, limit);
        Page<PlugHospital> result = hospitalService.findAll(null, maps, page);
        return JsonResponse.jsonOutput(response, 1, null, result);
    }


    @RequestMapping(value = "/hospital/detail.jspx")
    public String hospitalDetail(Integer id, HttpServletRequest request,
                                 HttpServletResponse response,
                                 org.springframework.ui.Model modelMap) {
        Site site = Context.getCurrentSite(request);
        Map<String, Object> data = modelMap.asMap();
        ForeContext.setData(data, request);
        if (id != null) {
            PlugHospital hospital = hospitalService.get(id);
            data.put("bean", hospital);
            List<String> builderList = new ArrayList<String>();
            if (hospital != null) {
                List<PlugHospitalDepartment> hospitalDepartments = hospitalDepartmentService.findByHospitalId(id);
                if (hospitalDepartments != null) {
                    StringBuilder sb = new StringBuilder();
                    for (PlugHospitalDepartment department : hospitalDepartments) {
                        sb.append("<div class=\"myytxt2\">");
                        sb.append("<h4>" + department.getfDepartmentName() + "</h4>");
                        HashMap<PlugDisease, List<PlugDoctor>> map = doctorService.findByDepartmentId(department.getId());
                        if (map != null) {
                            Iterator iter = map.entrySet().iterator();
                            while (iter.hasNext()) {
                                Map.Entry entry = (Map.Entry) iter.next();
                                PlugDisease key = (PlugDisease) entry.getKey();
                                List<PlugDoctor> val = (List<PlugDoctor>) entry.getValue();
                                sb.append("<p>");
                                sb.append(key.getfDiseaseName() + "（" + val.size() + "人）：");
                                for (int i = 0; i < val.size(); i++) {
                                    PlugDoctor doctor = val.get(i);
                                    if (i > 0) {
                                        sb.append("、");
                                    }
                                    sb.append("<a href='/doctor/detail.jspx?id=" + doctor.getId() + "'>" + doctor.getfDoctorName() + "</a>");
                                }
                                sb.append("</p>");
                            }
                        }
                        sb.append("</div>");
                    }
                    builderList.add(sb.toString());
                }
            }
            data.put("doctors", builderList);
        }
        return site.getTemplate("2—2名院详细.html");
    }

    @RequestMapping(value = "/doctor/detail.jspx")
    public String doctorDetail(Integer id, HttpServletRequest request,
                               HttpServletResponse response,
                               org.springframework.ui.Model modelMap) {

        Site site = Context.getCurrentSite(request);
        Map<String, Object> data = modelMap.asMap();
        ForeContext.setData(data, request);

        PlugDoctor doctor = doctorService.get(id);
        if (doctor != null) {
            data.put("doctor", doctor);
            data.put("department", doctor.getDepartment());
            data.put("hospital", doctor.getDepartment().getHospital());
            if (doctor.getDepartment() != null) {
                List<PlugDoctor> otherDoctors = doctorService.findOtherDoctors(doctor.getDepartment().getId(), id);
                if (otherDoctors != null) {
                    data.put("tongshi", otherDoctors);
                }
            }
        }
        return site.getTemplate("2—2名医详细.html");
    }


    @Autowired
    private PlugDoctorService doctorService;

    @Autowired
    private PlugDiseaseService diseaseService;

    @Autowired
    private PlugHospitalDepartmentService hospitalDepartmentService;

    @Autowired
    private PlugHospitalService hospitalService;
}
